<script type="text/javascript">
var paginationTotal = 1;// 记录总数
$(function() {
//			initData(0); // 初始化分页<{$lang.data}>

			$("#edit").dialog({
						autoOpen : false,
						width : 600,
						height : 400,
						modal : true,
						show : "slide",
						zIndex : 100
						
					});
			$(".messageDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>",
						buttons : {
							"<{$lang.confirm}>" : function() {
								$(this).dialog('close');
							},
							"<{$lang.close}>" : function() {
								$(this).dialog('close');
							}
						}
					});

			$("#confrimDiv").dialog({
						autoOpen : false,
						width : 400,
						height : 200,
						modal : true,
						show : "slide",
						zIndex : 200,
						title : "<{$lang.tip}><{$lang.msg}>"
					});
		});

function loadData(page, pageSize) {
	var st_code = $('[name=st_code]').val();
	var st_class_code = $('[name=st_class_code]').val();
	var st_name_cn = $('[name=st_name_cn]').val();
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type/index",
		data : {
			'page' : page,
			'pageSize' : pageSize,
			'st_code' : st_code,
			'st_class_code' : st_class_code,
			'st_name_cn' : st_name_cn
		},
		success : function(json) {
			paginationTotal = json.count;
			var html = "";
			if (json.count == 0) {
				html = "<td colspan='8' class=''>&nbsp;<{$lang.no_data}></td>";
			} else {
				$.each(json.list, function(key, item) {
					html += "<tr>";
					html += "<td>" + item.st_code + "</td>";
					html += "<td>" + item.st_name_cn + "</td>";
					html += "<td>" + item.st_name + "</td>";
					html += "<td>" + item.st_class_name + "</td>";
					html += "<td>" + item.st_status_name + "</td>";
					html += "<td><a href=\"javascript:editPage(" + item.st_id
							+ ");\"><{$lang.edit}></a></td>";
					html += "<td><a href=\"javascript:void(0)\" onclick=\"alertInfo("
							+ item.st_id
							+ ",'"
							+ item.st_code
							+ "')\"><{$lang.delete}></a></td>";
					html += "</tr>";
				})
			}
			$("#list").html(html);

		}
	});
}

function search() {
	initData(0);
}

function showEditPage(obj, titles, asc) {
	if (asc == 'add') {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.add}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	} else {
		obj.dialog({
					title : titles,
					buttons : {
						'<{$lang.edit}>' : function() {
							editSubmit()
						},
						'<{$lang.close}>' : function() {
							$(this).dialog('close');
						}
					}
				});
	}
	obj.dialog("open");
}
function editPage(st_id) {
	if (st_id == 0) {
		$('#edit [name=st_id]').val(0);
		$('#editForm').resetForm();
		showEditPage($('#edit'), '<{$lang.add}><{$lang.shipping_method}>', 'add');
		return;
	}
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type/info", // 请求的处理url
		data : {
			'st_id' : st_id
		},
		success : function(json) {
			if (!json.ask) {
				var html = '<p class="alertMsg"><img src="/images/not.gif"><{$lang.shipping_method}><{$lang.msg}><{$lang.get}><{$lang.fail}></p>';
				$('#message').html(html);
				$('.messageDiv').dialog("open");
				return false;
			}
			var row = json.list;

			$('#edit [name=st_id]').val(row.st_id);
			$('#edit [name=st_code]').val(row.st_code);
			$('#edit [name=st_name_cn]').val(row.st_name_cn);
			$('#edit [name=st_name]').val(row.st_name);
			$('#edit [name=st_deal_fee]').val(row.st_deal_fee);

			$('#edit [name=st_reg_fee]').val(row.st_reg_fee);
			$('#edit [name=st_addons]').val(row.st_addons);
			$('#edit [name=st_discount]').val(row.st_discount);
			$('#edit [name=st_delivery_time_min]')
					.val(row.st_delivery_time_min);

			$('#edit [name=st_delivery_time_max]')
					.val(row.st_delivery_time_max);
			$('#edit [name=st_vol_weight_rate]').val(row.st_vol_weight_rate);
			$("#edit [name='st_is_volume']").val([row.st_is_volume]);
			$('#edit [name=st_status]').val(row.st_status);

			$('#edit [name=st_class_code]').val(row.st_class_code);
			$('#edit [name=st_logo_image]').val(row.st_logo_image);
			$('#edit [name=warehouse_id]').val(row.warehouse_id);
			$('#edit [name=st_default_reply_address]')
					.val(row.st_default_reply_address);

			$('#edit [name=st_discount_min]').val(row.st_discount_min);
			$('#edit [name=st_deal_fee_min]').val(row.st_deal_fee_min);
			$('#edit [name=st_reg_fee_min]').val(row.st_reg_fee_min);
			$('#edit [name=st_sort]').val(row.st_sort);
			$('#edit [name=st_limit_volume]').val(row.st_limit_volume);
			showEditPage($('#edit'), '<{$lang.edit}><{$lang.shipping_method}>', 'edit');
		}
	});
}

function editSubmit() {
	var formData = $('#editForm').serialize();
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type/edit", // 请求的处理url
		data : formData,
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
			} else {
				var error = json.list.error;
				$.each(error, function(key, item) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ item + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
		}
	});

}

function alertInfo(st_id, st_code) {
	$("#confrimDiv").dialog({
				buttons : {
					"<{$lang.confirm}>" : function() {
						del(st_id, st_code)
					},
					"<{$lang.close}>" : function() {
						$(this).dialog('close')
					}
				}
			});
	$('#confrimMsg').html("<{$lang.confirm}><{$lang.delete}><{$lang.shipping_method}>" + st_code + '?');
	$('#confrimDiv').dialog("open");
}
function del(st_id, st_code) {
	$('#confrimDiv').dialog("close");
	$.ajax({
		type : "POST",
		async : false,
		dataType : "json",
		url : "/shipping/ship-type/delete", // 请求的处理url
		data : {
			'st_id' : st_id
		},
		success : function(json) {
			var html = "";
			if (json.ask) {
				var html = '<p class="alertMsg"><img src="/images/yes.gif">'
						+ json.list.message + '</p>';
				$('#message').html(html);
			} else {
				var error = json.list.error;
				$.each(error, function(key, item) {
							html += '<p class="alertMsg"><img src="/images/no.gif">'
									+ item + '</p>';
						});
				$('#message').html(html);
			}
			$('.messageDiv').dialog("open");
			var curPage = $(".pagination span> .current").html();
			initData(curPage);
		}
	});
}
</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>

<div id="iTitle">
	<div id="iTitleSub">
		<form id="searchForm" name="searchForm" >
		<div>
		<label>
		<{$lang.code}>：
        <input  class="myInput" type="text" name="st_code" onkeyup="if(event.keyCode==13){search();}" >
		</label>&nbsp;
        <label>
		<{$lang.chinese}><{$lang.name}>：
        <input  class="myInput" type="text" name="st_name_cn" onkeyup="if(event.keyCode==13){search();}" >
		</label>&nbsp;
        <label>
		<{$lang.type}>：
        <select  name="st_class_code">
            <option value=""><{$lang.pls}></option>
            <{foreach item="st_class_code" from=$stClassCodeList key='key'}>
            <option value="<{$key}>"><{$st_class_code}></option>
            <{/foreach}>
	    </select>
		</label>&nbsp;
		<input class="myButton" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="myButton" type="button" value="<{$lang.add}>"  id="addButton" onclick="editPage(0)">&nbsp;&nbsp;
        <input  type="hidden" value="<{$count}>" name="count">&nbsp;&nbsp;
		</div>
		</form>
	</div>
	<div id="tipmsg"></div>

</div>
<div class="listDiv ui-widget-content"></div>
<h3><{$lang.shipping}><{$lang.price}><{$lang.list}></h3>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
    <thead>
    <tr>
    <th><{$lang.code}></th>
    <th><{$lang.chinese}><{$lang.name}></th>
    <th><{$lang.name}></th>
    <th><{$lang.type}></th>
    <th><{$lang.status}></th>
    <th><{$lang.edit}></th>
    <th><{$lang.delete}></th>            
    </tr>
    </thead>
	<tbody id="list"></tbody>
</table>
<div class="pagination"></div>
<div style="display:none;" class="messageDiv">
    <div id="message"></div>
</div>

<div style="display:none;" id="confrimDiv">
    <div id="confrimMsg"></div>
</div>

<div style="display:none;" id="edit" >
    <{include file='shipping/shiptype/edit.tpl'}>
</div>